
layui.use(['element','jquery','form','table','layer','laydate','layedit'],function () {
    var element = layui.element;
    var $=layui.jquery;
    var form=layui.form;
    var table=layui.table;
    var layer=layui.layer;
    var laydate=layui.laydate;
    var layedit=layui.layedit;

    //日期时间选择器
    laydate.render({
        elem: '#startTime'
        ,type: 'datetime'
    });
    //日期时间选择器
    laydate.render({
        elem: '#endTime'
        ,type: 'datetime'
    });

    var tableIns = table.render({
        elem: '#mallUserTable',
        url: '/admin/mallUser/mallUserList',
        toolbar: '#mallUserToolBar',
        page: 'true',
        title: '用户数据表',
        cols: [ [
            {type:'checkbox'},
            {field:'id',title:'ID',align:'center'},
            {field:'userName',title:'用户名',align:'center'},
            {field:'nickName',title:'昵称',align:'center'},
            {field:'phone',title:'手机号',align:'center'},
            {field:'createTime',title:'注册时间',align:'center'},
            {field:'isLocked', title:'状态', width:110, templet: '#checkboxTpl', unresize: true},
        ] ],
        done:function (res,curr,count) {
            if(curr>1 && res.data.length==0){
                tableIns.reload({
                    page:{curr:curr-1}
                });

            }
        }
    });




    //监听锁定操作
    form.on('switch(lockedDemo)', function(obj){
        var id = this.value;
        var islocked = obj.elem.checked?2:1;
        $.post("/admin/mallUser/editById",{"id":id,"isLocked":islocked},function (res) {
            res.content;
        },"json")
    });


    form.on("submit(doSearch)",function (data) {
        tableIns.reload({
            url:'/admin/mallUser/mallUserList',
            where:data.field,
            page:{
                curr:1
            }
        })
        return false;
    })

    table.on('toolbar(mallUserTable)',function (obj) {
        switch (obj.event) {
            case "batchLockedtrue":
                batchLocked("true");
                break;
            case "batchLockedfalse":
                batchLocked("false");
                break;
        }
    })

    table.on('tool(mallUserTable)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case "show":
                showMallUser(data);
                break;
        }
    })

    //弹出层索引
    var mainIndex;

    //批量删除
    function batchLocked(is){
        //获取选中的行
        var checkStatus = table.checkStatus("mallUserTable");
        //判断是否选中行
        var length = checkStatus.data.length;
        if(length>0){
            layer.confirm('确定要修改这些数据吗?',{icon:3,title:"提示"},function (index) {
                var data = checkStatus.data;
                var idArr = []
                for(var i = 0 ; i<length;i++){
                    idArr.push(data[i].id);
                }
                var ids = idArr.join(",");
                console.log(ids)
                $.post("/admin/mallUser/batchLocked"+is,{"ids":ids},function (result) {
                    tableIns.reload();
                    layer.msg(result.content);
                },"json")
            })
            layer.close(index);
        }else{
            layer.alert("请选择要修改的行",{icon:0})
        }
    }


});
